package com.eleventwelve.iris.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.opentides.service.impl.BaseCrudServiceImpl;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.entity.PatientAllergy;
import com.eleventwelve.iris.service.PatientAllergyService;

public class PatientAllergyServiceImpl extends BaseCrudServiceImpl<PatientAllergy> implements PatientAllergyService   {

	
	
	@Override
	public void savePatientAllergies(Patient patient) {
		if(patient.getPatientAllergies()!=null && patient.getPatientAllergies().size()>0){
			List<PatientAllergy> patientAllergies = patient.getPatientAllergies();
			List<PatientAllergy> toDelete = new ArrayList<PatientAllergy>();
			
			for (PatientAllergy patientAllergy : patient.getPatientAllergies()) {
				if(!patientAllergy.getIsDeleted() && patientAllergy.getAllergen()!=null){
					patientAllergy.setPatient(patient);
				} else {
					toDelete.add(patientAllergy);
				}
			}
			
			patientAllergies.removeAll(toDelete);
			for (PatientAllergy patientAllergy : toDelete) {
				if(patientAllergy.getId()!=null){
					delete(patientAllergy.getId());
				}
			}
			
			getDao().saveAllEntityModel(patientAllergies);
		}
	}

	@Override
	public void saveAll(List<PatientAllergy> allergies) {
		getDao().saveAllEntityModel(allergies);
		
	}

}
